package com.fan.shop.dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Named;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.fan.shop.domain.PurchaseLine;
import com.fan.shop.domain.PurchaseOrder;

@Named("purchaseOrderDao")
public class PurchaseOrderDaoImpl implements PurchaseOrderDao {

	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 * 
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 * 
	 * @param SessionFactory
	 *            - Hibernate Session Factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public PurchaseOrder getPurchaseOrder(String poNumber) {
		Session session = sessionFactory.getCurrentSession();
		PurchaseOrder purchaseOrder = (PurchaseOrder) session.get(
				PurchaseOrder.class, poNumber);
		return purchaseOrder;
	}

	@SuppressWarnings("unchecked")
	public List<PurchaseOrder> getAllPurchaseOrders() {
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(PurchaseOrder.class);
		List<PurchaseOrder> list = (List<PurchaseOrder>) criteria.list();
		return list;
	}

	public void addPurchaseOrder(PurchaseOrder purchaseOrder) {
		Session session = sessionFactory.getCurrentSession();
		session.save(purchaseOrder);

	}

	public void updatePurchaseOrder(PurchaseOrder purchaseOrder) {
		Session session = sessionFactory.getCurrentSession();
		session.update(purchaseOrder);

	}

	public void deletePurchaseOrder(PurchaseOrder purchaseOrder) {
		Session session = sessionFactory.getCurrentSession();
		session.delete(purchaseOrder);

	}

	public boolean hasPurchaseOrder(String poNumber) {
		if (getPurchaseOrder(poNumber) == null) {
			return false;
		} else {
			return true;
		}
	}

	public List<String> getColumnNames() {
		List<String> columns = new ArrayList<String>();
		columns.add("PO Number");
		columns.add("Store Code");
		columns.add("Officer Code");
		columns.add("Transaction Code");
		columns.add("Transaction Date");
		columns.add("Buyer");
		columns.add("Delivery Address");
		columns.add("Postage");
		columns.add("Status");
		columns.add("Comments");
		return columns;
	}

	public BigDecimal getTotalPrice(String poNumber) {

		BigDecimal totalPrice = new BigDecimal(0.00);
		for (PurchaseLine purchaseLine : getPurchaseLinesByPoNumber(poNumber)) {
			BigDecimal rowPrice = purchaseLine.getPrice().multiply(
					new BigDecimal(purchaseLine.getQuantity()));
			totalPrice.add(rowPrice);
		}

		return totalPrice;
	}

	

	public int getTotalQty(String poNumber) {
		int totalQty = 0;
		for (PurchaseLine purchaseLine : getPurchaseLinesByPoNumber(poNumber)) {
			
			totalQty += purchaseLine.getQuantity();
		}
		return totalQty;
	}

	public List<PurchaseLine> getPurchaseLines(String poNumber) {
		return getPurchaseLinesByPoNumber(poNumber);
	}
	
	@SuppressWarnings("unchecked")
	private List<PurchaseLine> getPurchaseLinesByPoNumber(String poNumber) {
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(PurchaseLine.class);
		criteria.add(Restrictions.eq("id.poNumber", poNumber));
		List<PurchaseLine> list = (List<PurchaseLine>) criteria.list();
		return list;
	}

}
